<?php
// Authenticate
require("class.AuthenticationManager.php");
require("class.CommandMenu.php");
if (!$authenticationManager->isLoggedIn() || !$authenticationManager->hasClearance(CLEARANCE_ADMINISTRATOR)) {
    Header("Location: login.php?redirect=$_SERVER[PHP_SELF]&clearanceRequired=Administrator");
    exit;
}

// Connect to database:
$dbh = dbConnect();

// Load local vars from superglobals:
if (isset($_REQUEST['month']))
{    
    $month = $_REQUEST['month'];
}
if (isset($_REQUEST['year']))
{    
    $year = $_REQUEST['year'];
}
if (isset($_REQUEST['mode']))
{    
    $mode = $_REQUEST['mode'];
}
if (isset($_REQUEST['query']))
{    
    $query = $_REQUEST['query'];
}

//run the query  
list($qh,$num) = dbQuery($query);

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once 'Classes/PHPExcel.php';


// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Insert Silkan Logo:
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('PHPExcel logo');
$objDrawing->setDescription('PHPExcel logo');
$objDrawing->setPath('./images/silkan.gif');
$objDrawing->setHeight(60);
$objDrawing->setCoordinates('B2');
//$objDrawing->setOffsetX(-10);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

// Merge Cells
$objPHPExcel->getActiveSheet()->mergeCells('B2:B4');

// Set document properties
$objPHPExcel->getProperties()->setCreator("TimeSheet.php")
							 ->setLastModifiedBy("TimeSheet.php")
							 ->setTitle("Office 2007 XLSX Test Document")
							 ->setSubject("Office 2007 XLSX Test Document")
							 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
							 ->setKeywords("office 2007 openxml php")
							 ->setCategory("Test result file");



$row = 10;
if ($num == 0) {
    // Add some data
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', 'No data !');
} else {
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('B10', 'Project title')
                ->setCellValue('C10', 'Task name')
                ->setCellValue('D10', 'User name')
                ->setCellValue('E10', 'Date')
				->setCellValue('F10', 'Duration');
	

    $row++;
	while ($data = dbResult($qh)) 
	{
	    
	    $current_project_title = utf8_encode(stripslashes($data["title"]));
		$current_task_name     = utf8_encode(stripslashes($data["name"]));
		$username              = utf8_encode(stripslashes($data["username"]));
		$start_date            = $data["start_date"];
		$duration              = $data["diff"] / 60 / 60 /8;
	    // Add some data
        $str = 'B'.$row.'';
		$objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValueByColumnAndRow(1, $row, $current_project_title);
		
		$objPHPExcel->getActiveSheet()->getStyle($str)->getBorders()->applyFromArray(
    		array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array( 'rgb' => '2B5714' ))));
                   
        $str = 'C'.$row.'';
        $objPHPExcel->setActiveSheetIndex(0)
				   ->setCellValueByColumnAndRow(2, $row, $current_task_name);
		$objPHPExcel->getActiveSheet()->getStyle($str)->getBorders()->applyFromArray(
    		array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array( 'rgb' => '2B5714' ))));
			
		$str = 'D'.$row.'';	
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValueByColumnAndRow(4, $row, $start_date);
		$objPHPExcel->getActiveSheet()->getStyle($str)->getBorders()->applyFromArray(
    		array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array( 'rgb' => '2B5714' ))));
			
	    $str = 'E'.$row.'';
        $objPHPExcel->setActiveSheetIndex(0)
					->setCellValueByColumnAndRow(5, $row, $duration);
		$objPHPExcel->getActiveSheet()->getStyle($str)->getBorders()->applyFromArray(
    		array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array( 'rgb' => '2B5714' ))));
					
		$str = 'F'.$row.'';
        $objPHPExcel->setActiveSheetIndex(0)			
					->setCellValueByColumnAndRow(3, $row, $username);
		$objPHPExcel->getActiveSheet()->getStyle($str)->getBorders()->applyFromArray(
    		array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array( 'rgb' => '2B5714' ))));			
				
				
				
	    $row++;
	}
}

// Set fonts

			
$objPHPExcel->getActiveSheet()->getStyle('B10')->getFont()->setSize(12);
$objPHPExcel->getActiveSheet()->getStyle('B10')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B10')->getBorders()->applyFromArray(
    		array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array( 'rgb' => '2B5714' ))));
$objPHPExcel->getActiveSheet()->getStyle('B10')->applyFromArray( 
            array( 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'BCCF00') )));
			
$objPHPExcel->getActiveSheet()->getStyle('C10')->getFont()->setSize(12);
$objPHPExcel->getActiveSheet()->getStyle('C10')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C10')->getBorders()->applyFromArray(
    		array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array( 'rgb' => '2B5714' ))));
$objPHPExcel->getActiveSheet()->getStyle('C10')->applyFromArray( 
            array( 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'BCCF00') )));
			
$objPHPExcel->getActiveSheet()->getStyle('D10')->getFont()->setSize(12);
$objPHPExcel->getActiveSheet()->getStyle('D10')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('D10')->getBorders()->applyFromArray(
    		array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array( 'rgb' => '2B5714' ))));
$objPHPExcel->getActiveSheet()->getStyle('D10')->applyFromArray( 
            array( 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'BCCF00') )));
			
$objPHPExcel->getActiveSheet()->getStyle('E10')->getFont()->setSize(12);
$objPHPExcel->getActiveSheet()->getStyle('E10')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('E10')->getBorders()->applyFromArray(
    		array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array( 'rgb' => '2B5714' ))));
$objPHPExcel->getActiveSheet()->getStyle('E10')->applyFromArray( 
            array( 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'BCCF00') )));			

$objPHPExcel->getActiveSheet()->getStyle('F10')->getFont()->setSize(12);
$objPHPExcel->getActiveSheet()->getStyle('F10')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('F10')->getBorders()->applyFromArray(
    		array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array( 'rgb' => '2B5714' ))));
$objPHPExcel->getActiveSheet()->getStyle('F10')->applyFromArray( 
            array( 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'BCCF00') )));
			
			
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(11);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);

				
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Report_specific_user');


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Report_specific_user.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
